package jkCore.nativeNotification;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import com.google.gson.Gson;
import com.puzzlegames.jigsaw.puzzles.free.R;

import org.cocos2dx.javascript.AppActivity;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@SuppressLint("SpecifyJobSchedulerIdRange")
public class NotificationService extends JobService {
    private static String TAG = "NotificationService";
    SharedPreferences sharedPreferences;

    @Override
    public boolean onStartJob(JobParameters params) {
        Log.d(TAG, "onStartJob jobId = " + params.getJobId());

        sharedPreferences = getSharedPreferences("SHARE_PREFERENCE_NOTIFICATION", Context.MODE_PRIVATE);
        String id = "JKNotice_" + params.getJobId();
        String json = sharedPreferences.getString(id, "-1");
        Gson gson = new Gson();
        NotifyData notifyData = gson.fromJson(json, NotifyData.class);
        String title = notifyData.title;
        String msg = notifyData.msg;
        long time = notifyData.time;
        long period = notifyData.period;

        Log.d(TAG, "onStartJob time = " + time + " now = " + System.currentTimeMillis());
        if (System.currentTimeMillis() >= time) {
            // 更新下次触发时间
            notifyData.time = time + period;
            json = gson.toJson(notifyData);
            sharedPreferences.edit().putString(id, json).apply();

            NotificationManager mNotifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            //准备intent
            Intent notifyIntent = new Intent(this, AppActivity.class);
            Notification notification = null;
            // 构建 PendingIntent
            PendingIntent pi = PendingIntent.getActivity(this, 1, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            //版本兼容
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//兼容Android8.0
                int importance = NotificationManager.IMPORTANCE_HIGH;
                CharSequence name = "important notice";
                NotificationChannel mChannel = new NotificationChannel(id, name, importance);
                mChannel.enableLights(true);
                mChannel.setDescription("This is a important notice!");
                mChannel.setLightColor(Color.GREEN);
                mChannel.enableVibration(true);
                mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
                mChannel.setShowBadge(true);
                mNotifyMgr.createNotificationChannel(mChannel);

                Notification.Builder builder = new Notification.Builder(this, id);
                builder.setAutoCancel(true)
                        .setContentIntent(pi)
                        .setContentTitle(title)
                        .setContentText(msg)
                        .setOngoing(false)
                        .setSmallIcon(R.mipmap.ic_launcher_alpha)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                        .setWhen(System.currentTimeMillis())
                        .setPriority(Notification.PRIORITY_HIGH)
                        .setChannelId(id);
                notification = builder.build();
            } else if (Build.VERSION.SDK_INT >= 23) {
                NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
                builder.setContentTitle(title)
                        .setContentText(msg)
                        .setSmallIcon(R.mipmap.ic_launcher_alpha)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                        .setContentIntent(pi)
                        .setAutoCancel(true)
                        .setOngoing(false)
                        .setWhen(System.currentTimeMillis())
                        .setPriority(Notification.PRIORITY_HIGH)
                        .setChannelId(id);
                notification = builder.build();
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN &&
                    Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
                Notification.Builder builder = new Notification.Builder(this);
                builder.setAutoCancel(true)
                        .setContentIntent(pi)
                        .setContentTitle(title)
                        .setContentText(msg)
                        .setOngoing(false)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                        .setWhen(System.currentTimeMillis())
                        .setPriority(Notification.PRIORITY_HIGH);
                notification = builder.build();
            }
            if (notification != null) {
                Log.d(TAG, "onStartJob notify");
                mNotifyMgr.notify(params.getJobId(), notification);
//                startForeground(params.getJobId(), notification);
            } else {
                Log.e(TAG, "notification == null");
            }
        }

        return false;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Log.d(TAG, "onStopJob");

        return true;
    }
}
